# coding=UTF-8
import os

base_path = os.getcwd().split('\\StockMonitor')[0] + '\\StockMonitor\\'
stock_path = base_path.split('\\stock')[0] + '\\stock\\'

from utils import json_util, k_util


# 判断形态
def judge_form(rsi_list):
    # rsi_info = {'rq': start_rq, 'rsi_6': rsi_6, 'rsi_12': rsi_12, 'rsi_24': rsi_24,
    #             'a_6': a_6, 'b_6': b_6, 'a_12': a_12, 'b_12': b_12, 'a_24': a_24, 'b_24': b_24, }
    form = ''
    msg = ''
    form_info = {'form': form, 'msg': msg}
    # todo whm 后期优化
    if len(rsi_list) < 2:
        return form_info
    rsi_info = rsi_list[0]
    r6 = rsi_info['rsi_6']
    r12 = rsi_info['rsi_12']
    r24 = rsi_info['rsi_24']
    if r6 > 80:
        form += ':6-'
        msg += '~r6极强卖出'
    if r12 > 80:
        form += ':12-'
        msg += '~r12极强卖出'
    if r24 > 80:
        form += ':24-'
        msg += '~r24极强卖出'
    if r6 >= 50 and r6 < 80:
        form += ':6+'
        msg += '~r6强买入'
    if r12 >= 50 and r12 < 80:
        form += ':12+'
        msg += '~r12强买入'
    if r24 >= 50 and r24 < 80:
        form += ':24+'
        msg += '~r24强买入'
    rsi_info1 = rsi_list[1]
    r16 = rsi_info1['rsi_6']
    r112 = rsi_info1['rsi_12']
    if r6 > r12 and r16 < r112:
        # 当短期RSI线在低位向上突破长期RSI线时，一般为RSI指标的“黄金交叉”，为买入信号'
        form += ':X+'
        msg += '~黄金交叉'
    if r6 < r12 and r16 > r112:
        # 当短期RSI线在低位向上突破长期RSI线时，一般为RSI指标的“黄金交叉”，为买入信号'
        form += ':X-'
        msg += '~死亡交叉'
    form_info = {'form': form, 'msg': msg}
    return form_info


# todo whm 形态和算法不准确
# RSI的变动范围在0—100之间，强弱指标值一般分布在20—80。(如下图所示)
# RSI值市场特征投资操作
# 80—100极强卖出
# 50—80强买入
# 20—50弱观望
# 0—20极弱买入
# 这里的“极强”、“强”、“弱”和“极弱”只是一个相对的分析概念，是一个相对的区域。
# 有的投资者也可把它们取值为30、70或15、85，根据个人的喜好吧，没有绝对性。

# 交叉情况
# 短期RSI是指参数相对小的RSI,长期RSI是指参数相对较长的RSI。
# 比如,6日RSI和12日RSI中 ,6日RSI即为短期RSI,12日RSI即为长期RSI。长短期RSI线的交叉情况可以作为我们研判行情的方法。
# 1.当短期RSI>长期RSI时，市场则属于多头市场；
# 2.当短期RSI<长期RSI时，市场则属于空头市场；
# 3.当短期RSI线在低位向上突破长期RSI线时，一般为RSI指标的“黄金交叉”，为买入信号；
# 4.当短期RSI线在高位向下突破长期RSI线时，一般为RSI指标的“死亡交叉”，为卖出信号。

# 获取 rsi 最多支持天数120天
# start_rq : 开始日期 格式'2020-11-02' 倒序
# size   : 开始日期往前几天
# 注意事项: 计算值和平台给值有一定得差距
def get_rsi_interval(code, start_rq, size):
    # RSI的计算公式实际上就是反映了某一阶段价格上涨所产生的波动占总的波动的百分比率，百分比越大，
    # 强势越明显；百分比越小，弱势越明显。RSI的取值介于0—100
    # 之间。在计算出某一日的RSI值以后，可采用平滑运算法计算以后的RSI值，根据RSI值在坐标图上连成的曲线，即为RSI线。
    day_k_result = k_util.day_k_local(code, size + 25)
    day_k_use = []
    begin_flag = 0
    for i in range(len(day_k_result)):
        day_k = json_util.info_to_json(day_k_result[i])
        if begin_flag == 1:
            day_k_use.append(day_k)
            continue
        if day_k['rq'] == start_rq:
            begin_flag = 1
            day_k_use.append(day_k)
    a_6 = 0
    b_6 = 0
    a_12 = 0
    b_12 = 0
    a_24 = 0
    b_24 = 0
    rsi_list = []
    if len(day_k_use) < 25 + size:  # 数量不够计算 todo whm 后期优化
        return rsi_list
    for i in range(24):
        sp1 = float(day_k_use[i]['sp'])
        sp2 = float(day_k_use[i + 1]['sp'])
        diff = sp1 - sp2
        if diff > 0:
            a_24 = a_24 + diff
        else:
            b_24 = b_24 + diff
        if i < 6:
            if diff > 0:
                a_6 = a_6 + diff
            else:
                b_6 = b_6 + diff
        if i < 12:
            if diff > 0:
                a_12 = a_12 + diff
            else:
                b_12 = b_12 + diff
    # RSI = 100 - [100 / (1 + RS)]
    # 其中 RS = 14 天内收市bai价上涨数之和的平均值du / 14 天内收市价下跌数之和的平均值
    rsi_6 = a_6 / (a_6 - b_6) * 100
    rsi_12 = a_12 / (a_12 - b_12) * 100
    rsi_24 = a_24 / (a_24 - b_24) * 100
    rsi_info = {'rq': start_rq, 'rsi_6': rsi_6, 'rsi_12': rsi_12, 'rsi_24': rsi_24,
                'a_6': a_6, 'b_6': b_6, 'a_12': a_12, 'b_12': b_12, 'a_24': a_24, 'b_24': b_24, }
    rsi_list.append(rsi_info)
    if size == 1:
        return rsi_list
    for i in range(1, size):
        rq = day_k_use[i]['rq']
        diff0 = float(day_k_use[i - 1]['sp']) - float(day_k_use[i]['sp'])
        diff6 = float(day_k_use[i + 5]['sp']) - float(day_k_use[i + 6]['sp'])
        diff12 = float(day_k_use[i + 11]['sp']) - float(day_k_use[i + 12]['sp'])
        diff24 = float(day_k_use[i + 23]['sp']) - float(day_k_use[i + 24]['sp'])
        if diff0 > 0:
            a_6 -= diff0
            a_12 -= diff0
            a_12 -= diff0
        else:
            b_6 -= diff0
            b_12 -= diff0
            b_24 -= diff0
        if diff6 > 0:
            a_6 += diff6
        else:
            b_6 += diff6
        if diff12 > 0:
            a_12 += diff12
        else:
            b_12 += diff12
        if diff24 > 0:
            a_24 += diff24
        else:
            b_24 += diff24
        rsi_6 = a_6 / (a_6 - b_6) * 100
        rsi_12 = a_12 / (a_12 - b_12) * 100
        rsi_24 = a_24 / (a_24 - b_24) * 100
        rsi_info = {'rq': rq, 'rsi_6': rsi_6, 'rsi_12': rsi_12, 'rsi_24': rsi_24,
                    'a_6': a_6, 'b_6': b_6, 'a_12': a_12, 'b_12': b_12, 'a_24': a_24, 'b_24': b_24, }
        rsi_list.append(rsi_info)
    return rsi_list


if __name__ == '__main__':
    rsi_list = get_rsi_interval('600019', '2020-12-25', 10)
    for boo in rsi_list:
        print(str(boo))
